"""

JSON数据和python字典的相互转换
"""
import json

# 准备列表，列表内的每一个元素都是字典，将其转换为JSON
data = [{"name": "张三", "age": 18}, {"name": "李四", "age": 20}, {"name": "小王", "age": 21}]
json_str = json.dumps(data, ensure_ascii=False)
print(type(json_str))
print(json_str)

# 准备字典，将字典转换为JSON
d = {"name": "小黑", "addr": "中国"}
json_str = json.dumps(d, ensure_ascii=False)
print(type(json_str))
print(json_str)

# 将JSON字符串转换为Python数据类型[{k:v,k:v},{k:v,k:v}]
s = '[{"name": "张三", "age": 18}, {"name": "李四", "age": 20}, {"name": "小王", "age": 21}]'
l = json.loads(s)
print(type(l))
print(l)
# 将JSON字符串转换为Python数据类型：{k:v,k:v}
d = '{"name": "小黑", "addr": "中国"}'
json.loads(d)
print(type(d))
print(d)
